package com.prp.db.daoimpl.mysql;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.db.DAOFactory;
import com.prp.db.idao.IUserRecommendationDAO;
import com.prp.db.po.UserRecommendation;
import com.prp.db.po.UserState;

public class UserRecommendationDAO extends BaseHibernateDAO implements
		IUserRecommendationDAO {
	private static final Log log = LogFactory.getLog(UserRecommendation.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String FROM_ID = "fromId";
	public static final String TO_ID = "toId";

	@Override
	public void delete(UserRecommendation persistentInstance) {
		log.debug("deleting UserRecommendation instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public List findAll() {
		log.debug("finding all UserRecommendation instances");
		try {
			String queryString = "from UserRecommendation";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public List findByFromId(Object fromId) {
		return findByProperty(FROM_ID, fromId);
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserRecommendation instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from UserRecommendation as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public boolean findByExample(int userId, int fromId, int toId) {
		try {
			String queryString = "from UserRecommendation as model where model."
					+ USER_ID
					+ "= ? and model."
					+ FROM_ID
					+ "=? and model."
					+ TO_ID + "=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userId);
			queryObject.setParameter(1, fromId);
			queryObject.setParameter(2, toId);
			return queryObject.list().size() > 0;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByToId(Object toId) {
		return findByProperty(TO_ID, toId);
	}

	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	@Override
	public void save(UserRecommendation transientInstance) {
		log.debug("saving UserRecommendation instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public UserRecommendation findById(Integer id) {
		log.debug("getting UserRecommendation instance with id: " + id);
		try {
			UserRecommendation instance = (UserRecommendation) getSession()
					.get("com.prp.db.po.UserRecommendation", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

}
